package com.example.server.config;

import com.example.server.security.*;
import com.example.server.security.authorize.HasAnyRoleAuthorizeDecider;
import com.example.server.security.authorize.JdbcResourceMetaDataProvider;
import com.example.server.security.context.SessionBaesdSecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration // 交给spring 管理； 定位： 配置类
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${yeb.security.contextType}")
    private String contextType;

    @Autowired
    private AuthenticatedInterceptor authenticatedInterceptor;

    @Autowired
    private AuthorizeInterceptor authorizeInterceptor;

//    @Autowired
//    private SessionBaesdSecurityContextHolder sessionBaesdSecurityContextHolder;

    @Autowired
    private JdbcResourceMetaDataProvider jdbcResourceMetaDataProvider;

    @Autowired
    private HasAnyRoleAuthorizeDecider hasAnyRoleAuthorizeDecider;

    @Autowired
    private RequiredInterceptor requiredInterceptor;

    @Autowired
    private TokenLoadingInterceptor tokenLoadingInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) { // 添加拦截器

//        if (contextType == null || "".equals(contextType) || "session".equals(contextType)) {
//
//            authenticatedInterceptor.setSecurityContextHolder(sessionBaesdSecurityContextHolder);
//            authorizeInterceptor.setSecurityContextHolder(sessionBaesdSecurityContextHolder);
//        } else if ("token".equals(contextType)) {
//
//        }

        authorizeInterceptor.setResourceMetaDataProvider(jdbcResourceMetaDataProvider);
        authorizeInterceptor.setAuthorizeDecider(hasAnyRoleAuthorizeDecider);

        if ("token".equals(contextType)) {
            registry.addInterceptor(tokenLoadingInterceptor).addPathPatterns("/**")
                    .excludePathPatterns("/login")
                    .excludePathPatterns("/captcha")
                    .excludePathPatterns("/doc.html")
                    .excludePathPatterns("/webjars/**")
                    .excludePathPatterns("/asserts/**")
                    .excludePathPatterns("/swagger-resources");
//            setDefaultPath(registry,tokenLoadingInterceptor);
        }

//        registry.addInterceptor(authenticatedInterceptor).addPathPatterns("/**")
//                .excludePathPatterns("/login")
//                .excludePathPatterns("/captcha")
//                .excludePathPatterns("/doc.html")
//                .excludePathPatterns("/webjars/**")
//                .excludePathPatterns("/asserts/**")
//                .excludePathPatterns("/swagger-resources");
        setDefaultPath(registry,authenticatedInterceptor);
//
//        registry.addInterceptor(authorizeInterceptor).addPathPatterns("/**")
//                .excludePathPatterns("/login")
//                .excludePathPatterns("/captcha")
//                .excludePathPatterns("/doc.html")
//                .excludePathPatterns("/webjars/**")
//                .excludePathPatterns("/asserts/**")
//                .excludePathPatterns("/swagger-resources");
        setDefaultPath(registry,authorizeInterceptor);

//        registry.addInterceptor(requiredInterceptor).addPathPatterns("/**")
//                .excludePathPatterns("/login")
//                .excludePathPatterns("/captcha")
//                .excludePathPatterns("/doc.html")
//                .excludePathPatterns("/webjars/**")
//                .excludePathPatterns("/asserts/**")
//                .excludePathPatterns("/swagger-resources");
        setDefaultPath(registry,requiredInterceptor);


    }

    private void setDefaultPath(InterceptorRegistry registry, HandlerInterceptor handlerInterceptor){
        registry.addInterceptor(requiredInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/captcha")
                .excludePathPatterns("/doc.html")
                .excludePathPatterns("/webjars/**")
                .excludePathPatterns("/asserts/**")
                .excludePathPatterns("/swagger-resources");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 添加静态资源

        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}